package com.yuke.cloud.service.wmc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.pmc.dto.ProdCategoryDto;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.AccEmployeeInfoDto;
import com.yuke.cloud.service.api.uac.dto.SupplierApiDto;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.wmc.dao.WmsPurchaseOrderMapper;
import com.yuke.cloud.service.wmc.dao.WmsPurchaseReturnOrderMapper;
import com.yuke.cloud.service.wmc.dao.WmsStockInMapper;
import com.yuke.cloud.service.wmc.dto.*;
import com.yuke.cloud.service.wmc.entity.*;
import com.yuke.cloud.service.wmc.service.WmsPurchaseOrderService;
import com.yuke.cloud.service.wmc.service.WmsStockInService;
import com.yuke.cloud.service.wmc.service.WmsStorageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * Created by zn on 2020/10/08.
 */
@Service
@Transactional
public class WmsPurchaseOrderServiceImpl extends BaseService<WmsPurchaseOrder> implements WmsPurchaseOrderService {
    @Resource
    private WmsPurchaseOrderMapper wmsPurchaseOrderMapper;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private PmcFeignApi pmcFeignApi;
    @Resource
    private WmsStockInService wmsStockInService;
    @Resource
    private WmsStorageService wmsStorageService;
    @Resource
    private WmsStockInMapper wmsStockInMapper;
    @Resource
    private WmsPurchaseReturnOrderMapper wmsPurchaseReturnOrderMapper;



    @Override
    public List<WmsPurchaseOrderDto> selectCondition(WmsPurchaseOrderDto wmsPurchaseOrderDto) {

        List<WmsPurchaseOrderDto> list = wmsPurchaseOrderMapper.selectCondition(wmsPurchaseOrderDto);

        List<Long> supplierIds = list.stream().map(WmsPurchaseOrderDto::getSupplierId).collect(Collectors.toList());

        if (list != null && list.size()>0) {
            List<SupplierApiDto> supplierList = uacUserFeignApi.findSupplierByIds(supplierIds);
            if (supplierList == null || supplierList.size() < 1){
                return list;
            }
            AccEmployeeInfoDto accEmployeeInfoDto = new AccEmployeeInfoDto();
            accEmployeeInfoDto.setEmployeeType((byte)0);
            Wrapper<List<UserInfoDto>> userWrapper =uacUserFeignApi.selectEmployInfo(accEmployeeInfoDto);
            if (userWrapper.getCode() != 200){
                return list;
            }
            List<UserInfoDto> userList = userWrapper.getResult();
            for (WmsPurchaseOrderDto purchaseOrderDto : list) {
                for (SupplierApiDto supplierApiDto : supplierList) {
                    if (purchaseOrderDto.getSupplierId()!=null && purchaseOrderDto.getSupplierId()>0) {
                        if (purchaseOrderDto.getSupplierId().equals(supplierApiDto.getSupplierId())) {
                            BeanUtils.copyProperties(supplierApiDto, purchaseOrderDto);
                        }
                    }
                }
                for (UserInfoDto userInfoDto : userList){
                    if (purchaseOrderDto.getUpdateId().equals(userInfoDto.getUserId())){
                        purchaseOrderDto.setUserName(userInfoDto.getUserName());
                        break;
                    }
                }
            }
        }

        for (WmsPurchaseOrderDto purchaseOrderDto : list) {
            System.out.println("purchaseOrderDto"+purchaseOrderDto);
        }
        return list;
    }

    @Override
    public List<WmsPurchaseOrderDto> listAndOther() {

        //查询所有
        List<WmsPurchaseOrderDto> wmsPurchaseOrders = wmsPurchaseOrderMapper.selectCondition(null);

        if (wmsPurchaseOrders.size()>0&&wmsPurchaseOrders!=null) {
            //取出所有
            List<Long> supplierIds = wmsPurchaseOrders.stream().map(WmsPurchaseOrderDto::getSupplierId).collect(Collectors.toList());

            List<SupplierApiDto> supplierApiDto = uacUserFeignApi.findSupplierByIds(supplierIds);

            for (WmsPurchaseOrderDto wmsPurchaseOrder : wmsPurchaseOrders) {
                for (SupplierApiDto supplier : supplierApiDto) {
                    if (wmsPurchaseOrder.getSupplierId().equals(supplier.getSupplierId())) {
                        BeanUtils.copyProperties(supplier,wmsPurchaseOrder);
                    }
                 }
            }
        }
        return wmsPurchaseOrders;
    }


    @Override
    public List<WmsStockInPurchaseDto> findGoodsByPurchase(Long purchaseId) {

        List<WmsStockInPurchaseDto> list = wmsPurchaseOrderMapper.findGoodsByPurchase(purchaseId);

        return list;
    }

    @Override
    public PageInfo<WmsStatisticsDto> costUnitPrice(WmsStatisticsDto wmsStatisticsDto, Integer page, Integer size) {

        if (wmsStatisticsDto.getParentId()!=null&&!wmsStatisticsDto.getParentId().equals(0L)) {

            ProdCategoryDto prodCategoryDto = new ProdCategoryDto();
            //根据商品分类
            prodCategoryDto.setParentId(wmsStatisticsDto.getParentId());

            List<Long> leaf = pmcFeignApi.findLeaf(prodCategoryDto);

            wmsStatisticsDto.setCategoryIdList(leaf);
            wmsStatisticsDto.setCategoryId(null);
        }
        wmsStatisticsDto.setInType((byte) 0);

        PageHelper.startPage(page, size);

        List<WmsStatisticsDto> statisticsList = wmsStockInMapper.costUnitPriceByList(wmsStatisticsDto);

        PageInfo<WmsStatisticsDto> result = new PageInfo<>(statisticsList);

        return result;
    }


    @Override
    public List<PaymentOrderDto> findAllPayment(PaymentOrderDto paymentOrderDto) {

        List<PaymentOrderDto> list = wmsPurchaseOrderMapper.findAllPayment(paymentOrderDto);

        List<Long> specList = list.stream().map(PaymentOrderDto::getSupplierId).collect(Collectors.toList());

        //找对应供应商名称
        List<SupplierApiDto> supplierByIds = uacUserFeignApi.findSupplierByIds(specList);

        for (PaymentOrderDto orderDto : list) {
            for (SupplierApiDto supplierById : supplierByIds) {
                BeanUtils.copyProperties(supplierById,orderDto);
            }
        }
        return list;
    }

    @Override
    public Integer deleteStorage(Long id) {

        //修改入库记录
        //WmsStockIn wmsStockIn = new WmsStockIn();
        Example example = new Example(WmsStockIn.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("purchaseId", id);
        List<WmsStockIn> list = wmsStockInService.selectByExample(example);

        for (WmsStockIn stockIn : list) {
            stockIn.setIsDelete(true);
            Integer num = wmsStockInService.update(stockIn);
            if (num>0) {
                //根据list 更新wmsStorage表内库存数
                WmsStorage wmsStorage = new WmsStorage();
                wmsStorage.setSpecId(stockIn.getSpecId());
                wmsStorage.setWarehouseId(stockIn.getWarehouseId());
                List<WmsStorage> select = wmsStorageService.select(wmsStorage);
                wmsStorage = select.get(0);
                Integer number = wmsStorage.getNumber() - stockIn.getInNumber();
                wmsStorage.setNumber(number);////进货退货取消后 应该减去库存
                wmsStorage.setUpdateTime(new Date());
                Integer update = wmsStorageService.update(wmsStorage);
                if (update.equals(0)) {
                    return 0;
                }
            }else if (num.equals(0)){
                return 0;
            }
        }
        return 1;
    }

    @Override
    public List<FlowCountDto> countMoney(WmsPurchaseOrderDto wmsPurchaseOrderDto) {
        //查出供应商对应的应付payable
        List<FlowCountDto> list = wmsPurchaseOrderMapper.countMoney(wmsPurchaseOrderDto);
        System.out.println("countMoney====="+list);
        List<Long> collect = list.stream().map(FlowCountDto::getSupplierId).collect(Collectors.toList());

        //进货退货单的实付 应付
        WmsPurchaseReturnOrderDto wmsPurchaseReturnOrderDto = new WmsPurchaseReturnOrderDto();
        wmsPurchaseReturnOrderDto.setSupplierId(wmsPurchaseOrderDto.getSupplierId());
        List<FlowCountDto> returnList = wmsPurchaseReturnOrderMapper.countMoney(wmsPurchaseReturnOrderDto);

        //对应的供应商名称
        List<SupplierApiDto> supplierByIds = uacUserFeignApi.findSupplierByIds(collect);
        System.out.println("findSupplierByIds====="+supplierByIds);
        if (supplierByIds != null && supplierByIds.size() > 0) {
            for (FlowCountDto flowCountDto : list) {
                //供应商对应的进货退货金额
                if (supplierByIds !=null && supplierByIds.size() > 0 ) {
                    for (FlowCountDto countDto : returnList) {
                        if (countDto.getSupplierId().equals(flowCountDto.getSupplierId())) {
                            flowCountDto.setReceivable(countDto.getReceivable());
                            flowCountDto.setReceipts(countDto.getReceipts());
                        }
                    }
                }
                //找到供应商名字
                for (SupplierApiDto supplierById : supplierByIds) {
                    if (flowCountDto.getSupplierId().equals(supplierById.getSupplierId())) {
                        flowCountDto.setSupplierName(supplierById.getSupplierName());
                    }
                }
            }
        }
        return list;
    }



}
